# 1、导包
import unittest
import app
from api.wxmini import MiniProgram
import pymysql


# 2、创建测试类，继承自unittest.TestCase
class Mini(unittest.TestCase):
    #  初始化组件fixtrue
    @classmethod
    def setUpClass(cls) -> None:
        cls.mini = MiniProgram()

    def setUp(self) -> None:
        pass

    def tearDown(self) -> None:
        pass

    # 测试方法以test开头
    def test01_get_banner(self):
        # 获取轮播图测试
        response = self.mini.banner()
        print(f'轮播图的结果为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual('首页轮播图', response.json().get('description'))

    def test02_get_theme(self):
        response = self.mini.theme({'ids': '1,2,3'})
        print(f'专题栏位的结果：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual('专题栏位一', response.json()[0].get('name'))

    def test03_new_product(self):
        # 最近新品
        response = self.mini.new_product()
        print(f'最近新品结果为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual('芹菜 半斤', response.json()[0].get('name'))

    def test04_all(self):
        # 商品分类
        response = self.mini.category_all()
        print(f'商品分类：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual('果味', response.json()[0].get('name'))

    def test05_category(self):
        # 分类下的商品
        response = self.mini.by_category(2)
        print(f'分类下的商品详情为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual('梨花带雨 3个', response.json()[0].get('name'))

    def test06_product(self):
        # 查看商品id为2的商品详情
        response = self.mini.product('2')
        print(f'结果为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual('梨花带雨 3个', response.json().get('name'))

    def test07_user_order(self):
        # 查询订单列表
        response = self.mini.user_order(1, {'token': '5e8344375e209cb2730f7356ff7b4d0c'})
        print(f'结果为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json().get('current_page'))
        self.assertEqual(117, response.json().get('data')[0].get('id'))

    def test08_new_order(self):
        # 新建订单
        app.headers['token'] = '5e8344375e209cb2730f7356ff7b4d0c'
        response = self.mini.new_order({"products": [{"product_id": 8, "count": 1},
                                                     {"product_id": 10, "count": 2}]})
        self.assertEqual(200, response.status_code)

    def test_09_see_order(self):
        # 查看订单列表中117号的订单详情
        response = self.mini.see_order('117', {'token': '5e8344375e209cb2730f7356ff7b4d0c'})
        print(f'查看结果为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual(117, response.json().get('id'))

    def test_10_get(self):
        # 获取token
        app.headers1['Content-Type'] = 'application/json'
        response = self.mini.get_token({'code': '093N6j0w3VrHSU2bXg0w3s6tdu4N6j0s'})
        print(f'只能使用一次：{response.json()}')
        self.assertEqual(200, response.status_code)

    def test_11_check(self):
        # 验证token
        app.headers1['Content-Type'] = 'application/json'
        response = self.mini.check_token({'token': '5e8344375e209cb2730f7356ff7b4d0c'})
        print(f'结果为：{response.json()}')
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get('isValid'))

    def test_12_address(self):
        response = self.mini.address({'token': '5e8344375e209cb2730f7356ff7b4d0c'})
        print(f'地址信息为：{response.json()}')
        self.assertEqual(200, response.status_code)
